﻿using System;
using System.Linq;
using Loki.Engine;
using Loki.Model;
using Luna.Data;
using Luna.Data.Accounting;

namespace Luna.Model.Accounting
{
    internal class CacheConfiguration : CacheConfiguration<LunaDataModel>
    {
        public static readonly ContainerConfiguration<IAccountingCategory, Guid> CATEGORIES = new ContainerConfiguration<IAccountingCategory, Guid>()
        {
            LoadingErrorCode = ErrorCodes.ACC_010,
            FullLoader = () => DataContext.Accounting.GetAllCategories().Select(x => new AccountingCategory(x)),
            Indexes = null,
            PrimaryKey = new UniqueKeyConfiguration<IAccountingCategory, Guid>()
            {
                Indexer = u => u.ID,
                Loader = g => { INamedEntityData db = DataContext.Accounting.GetCategory(g); return db == null ? null : new AccountingCategory(db); }
            }
        };

        public static readonly ContainerConfiguration<IAccount, Guid> ACCOUNTS = new ContainerConfiguration<IAccount, Guid>()
        {
            LoadingErrorCode = ErrorCodes.ACC_011,
            FullLoader = () => DataContext.Accounting.GetAllAccounts().Select(x => new Account(x)),
            Indexes = null,
            PrimaryKey = new UniqueKeyConfiguration<IAccount, Guid>()
            {
                Indexer = u => u.ID,
                Loader = g => { IAccountData db = DataContext.Accounting.GetAccount(g); return db == null ? null : new Account(db); }
            }
        };

        public static readonly ContainerConfiguration<AccountOperation, Guid> OPERATIONS = new ContainerConfiguration<AccountOperation, Guid>()
        {
            LoadingErrorCode = ErrorCodes.ACC_012,
            FullLoader = () => DataContext.Accounting.GetAllOtherOperations().Select(x => new AccountOperation(x)),
            Indexes = new IIndexConfiguration<AccountOperation>[]
            {
                 new IndexConfiguration<AccountOperation, Guid>() { Indexer = x => x.AccountID }            
            },
            PrimaryKey = new UniqueKeyConfiguration<AccountOperation, Guid>()
            {
                Indexer = u => u.ID,
                Loader = g => { IAccountOperationData db = DataContext.Accounting.GetOtherOperation(g); return db == null ? null : new AccountOperation(db); }
            }
        };
    }
}
